package com.dq.service.service.impl;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.CusArrearsQuery;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.domain.org.Org;
import com.dq.domain.record.ArrearsRecord;
import com.dq.repository.BaseRepositories;
import com.dq.service.org.OrgService;
import com.dq.service.service.CusArrearsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * @author liuhui
 * @date 2019/11/25 0025 10:52:06
 */
@Service
public class CusArrearsServiceImpl extends BaseRepositories implements CusArrearsService {

    @Autowired
    private OrgService orgService;
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public List<ArrearsRecord> findByWhere(CusArrearsQuery cusArrearsQuery)throws AdminException {
        StringBuilder hql = new StringBuilder("select u from ArrearsRecord u where u.deleted=false");

        Query q = createWhereHql(cusArrearsQuery,hql);

        if(cusArrearsQuery.getPage() != null && cusArrearsQuery.getSize() != null) {
            q.setFirstResult((cusArrearsQuery.getPage()-1)*cusArrearsQuery.getSize());
            q.setMaxResults(cusArrearsQuery.getSize());
        }
        List<ArrearsRecord> arrearsRecordList = q.getResultList();
        return arrearsRecordList;
    }


    @Override
    public Long countByWhere(CusArrearsQuery cusArrearsQuery)throws AdminException {
        StringBuilder hql = new StringBuilder("select count(u) from ArrearsRecord u where u.deleted=false");
        Query q = createWhereHql(cusArrearsQuery,hql);
        return new Long((long) q.getResultList().get(0));
    }



    public Query createWhereHql(CusArrearsQuery cusArrearsQuery,StringBuilder hql) throws AdminException {
        if(null != cusArrearsQuery.getOrgId()){
            hql.append(" and u.sysDept=:org");
        }else {
            // 只显示负责门店信息
            String orgIdsIn = orgService.getIdListAllByUser();
            if(StringUtils.isNotBlank(orgIdsIn)) {
                hql.append(" and u.sysDept.id in ").append(orgIdsIn);
            }
        }

        if(null!=cusArrearsQuery.getOrgName()){
            hql.append(" and u.sysDept.name like:orgName");
        }

        if(StringUtils.isNotBlank(cusArrearsQuery.getCardNo())) {
            hql.append(" and u.sysUser.cardNo like '").append(BaseAdminQuery.likeSql(cusArrearsQuery.getCardNo())).append("'");
        }
        if(StringUtils.isNotBlank(cusArrearsQuery.getGetUserRealcName())) {
            hql.append(" and u.sysUser.userRealcName like '").append(BaseAdminQuery.likeSql(cusArrearsQuery.getGetUserRealcName())).append("'");
        }

        if(StringUtils.isNotEmpty(cusArrearsQuery.getValue())){
            hql.append(" and (u.sysUser.cardNo like:value");
            hql.append(" or u.sysUser.username like:value");
            hql.append(" or u.sysUser.userRealcName like:value)");
        }
        if(null!=cusArrearsQuery.getBeginTime()){
            hql.append(" and u.createTime >=:beginTime");
        }
        if(null!=cusArrearsQuery.getEndTime()){
            hql.append(" and u.createTime <=:endTime");
        }
        if(null!=cusArrearsQuery.getArrearsStatus()){
            hql.append(" and u.arrearsStatus =:status");
        }
        if(null!=cusArrearsQuery.getArrearsNo()){
            hql.append(" and u.arrearsNo like:arrearsNo");
        }
        if(null!=cusArrearsQuery.getWxUserId()){
            hql.append(" and u.sysUser.id =:sysUser");
        }
        hql.append(" ORDER BY u.createTime desc");
        Query query = entityManager.createQuery(hql.toString());

        if(null!=cusArrearsQuery.getOrgId()){
            Org org = dao.get(Org.class,cusArrearsQuery.getOrgId());
            if(org == null) {
                throw new AdminException("获取门店信息异常");
            }
            query.setParameter("org", org);
        }
        if(null!=cusArrearsQuery.getOrgName()){
            query.setParameter("orgName","%"+cusArrearsQuery.getOrgName()+"%");
        }
        if(StringUtils.isNotEmpty(cusArrearsQuery.getValue())){
            query.setParameter("value","%"+cusArrearsQuery.getValue()+"%");
        }
        if(null!=cusArrearsQuery.getBeginTime()){
            query.setParameter("beginTime",cusArrearsQuery.getBeginTime());
        }
        if(null!=cusArrearsQuery.getEndTime()){
            query.setParameter("endTime",cusArrearsQuery.getEndTime());
        }
        if(null!=cusArrearsQuery.getArrearsStatus()){
            query.setParameter("status",cusArrearsQuery.getArrearsStatus());
        }
        if(null!=cusArrearsQuery.getArrearsNo()){
            query.setParameter("arrearsNo","%"+cusArrearsQuery.getArrearsNo()+"%");
        }
        if(null!=cusArrearsQuery.getWxUserId()){
            query.setParameter("sysUser",cusArrearsQuery.getWxUserId());
        }
        return query;
    }

}
